<?php
$db2 i new dbquery;
if(!$_GET['cmd']) {
	if(!perms_check('users', 'gr_edit')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	//sort
	if(!$_GET['sort'])
		$_GET['sort']='ASC';
	if(!$_GET['sortby'])
		$_GET['sortby']='type';
	if($_GET['sort']=='ASC')
		$sortnext='DESC';
	else
		$sortnext='ASC';
	//

	$content = string_template(read_file('admin/themes/groups_header.php'), array("sortnext" => $sortnext));

	$zapytanie = "SELECT * FROM $conf[prefix_groups]groups";

	if($_GET['sortby'] == 'name' or $_GET['sortby'] == 'desc')
		$_GET['sortby'] .= "_$_SESSION[lang_short]";

	if($_GET['sort'])
		$zapytanie.= " ORDER BY ".$_GET['sortby']." $_GET[sort]";
	else
		$zapytanie.= " ORDER BY id ASC";

	$db->query($zapytanie) or $db->err(__FILE__, __LINE__);

	while($group = $db->fetch_object()) {
		$i++;

		$nm = "name_$_SESSION[lang_short]";
		$ds = "desc_$_SESSION[lang_short]";

		if($group->$nm == '') {
			$db2 = new dbquery;
			$db2->query("SELECT $conf[language] FROM $conf[prefix]lang WHERE name = 'lang'") or $db2->err(__FILE__, __LINE__);

			$lng = $db2->fetch_array();
			$lng = $lng[$conf['language']];

			$nm = "name_$lng";
			$ds = "desc_$lng";
		}

		$name = read_text_rest($group->$nm);
		$desc = read_text_rest($group->$ds);

		$ed = 1;

		switch($group->type) {
			case -1:	$type = $lang['groups_special'];
					$ed = 0;
					break;
			case 0:		$type = $lang['groups_normal'];
					break;
			case 1:		$type = $lang['groups_single_user'];
					break;
			case 2:		$type = $lang['groups_redactors'];
					break;
			case 3:		$type = $lang['groups_single_redactor'];
					break;
		}

		if(get_gid_by_uid($_SESSION['id']) == $group->id)
			$ed = 0;

		$content .= string_template(read_file('admin/themes/groups_item.php'), array("id" => $group->id, "name" => add_dots(strip_tags(stripslashes($name)), 30), "desc" => add_dots(strip_tags(stripslashes(read_text($desc))), 30), "date" => date($conf['date_format'], strtotime($group->date)), "type" => $type, "i" => (($i%2)+1), 'ed' => $ed));
	}
	$content .= read_file('admin/themes/groups_footer.php');
}
elseif($_GET['cmd']=='edit') {

	if(!perms_check('users', 'gr_edit')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	SQLvalidate($_GET['id']);
	$db->query("SELECT * FROM $conf[prefix_groups]groups WHERE id=$_GET[id]") or $db->err(__FILE__, __LINE__);

	//sprawdzanie czy grupa istnieje
	if($db->num_rows() == 0) {
		redirect('index.php?module=admin&action=groups');
		exit;
	}
	//

	$d = $db->fetch_object();

	$ed = 1;
	if($d->type == -1)
		$ed = 0;

	$d->image=stripslashes($d->image);

	if($ed == 1) {
		$type ="<option value=\"1\"";
		if($d->type == 1)
			$type .= ' selected';
		$type .= ">$lang[groups_single_user]</option>
			<option value=\"2\"";
		if($d->type == 2)
			$type .= ' selected';
		$type .= ">$lang[groups_redactors]</option>";
	}

	$db2->query("SELECT * FROM $conf[prefix_ranks]ranks WHERE points = -1 ORDER BY name_$_SESSION[lang_short] ASC") or $db->err(__FILE__, __LINE__);

	$selected = NULL;
	if($d->rank == 0)
		$selected = ' selected';
	$rank = "<option value=\"0\"$selected>$lang[ranks_std]</option>\n";

	$rank .= "<option value=\"sep\">-------------</option>\n";

	while($rk = $db2->fetch_array()) {
		$selected = NULL;
		if($d->rank == $rk['id'])
			$selected = ' selected';

		$nm = "name_$_SESSION[lang_short]";

		if($rk[$nm] == '') {
			$db2 = new dbquery;
			$db2->query("SELECT $conf[language] FROM $conf[prefix]lang WHERE name = 'lang'") or $db2->err(__FILE__, __LINE__);

			$lng = $db2->fetch_array();
			$lng = $lng[$conf['language']];

			$nm = "name_$lng";
		}

		$rk[$nm] = read_text_rest($rk[$nm]);
		$rank .= "<option value=\"$rk[id]\"$selected>$rk[$nm]</option>\n";
	}

	//current_moderate_options
	$cmo = explode(';', $d->moderator);

	//add_moderator_options
	if(perms_check('forum', 'set_moderators'))
		$moderator_options=get_options_for_user_edit_moderators('', '', '&nbsp;&nbsp;', '->&nbsp;', '0', $cmo);
	//

	$array = array('ed' => $ed, 'rank' => $rank, 'function' => 'group_edit', 'id' => $d->id, 'type' => $type, 'date' => date('Y-m-d H:i:s', strtotime($d->date)), 'type' => $type, 'color' => $d->color, 'submit' => $lang['admin_edit'], 'moderator_options'=>$moderator_options, 'prefix'=>$d->prefix);

	$multi = NULL;

	$langs = get_lang_list();
	$languages = get_languages_list();

	$i = 0;

	foreach($langs as $key => $l) {
		$i++;
		if($l==$_SESSION['lang_short']) {
			$icon = 'collapse';
			$style = 'block';
		}
		else {
			$icon = 'expand';
			$style = 'none';
		}

		$multi .= string_template(read_file('admin/themes/groups_form_multi.php'), array('lang' => $l, 'language' => strtoupper($l), 'icon' => $GLOBALS['theme_path'].$GLOBALS['theme_img'][$icon], 'style' => $style, 'i' => $i));
		$nm = "name_$l";
		$ds = "desc_$l";
		$array[$nm] = stripslashes($d->$nm);
		$array[$ds] = stripslashes($d->$ds);
	}

	$content = string_template(read_file('admin/themes/groups_form.php'), array('multi' => $multi));
	$content = string_template($content, $array);
}
elseif($_GET['cmd'] == 'delete') {

	if(!perms_check('users', 'gr_delete')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}
	SQLvalidate($_GET['id']);
	$db->query("SELECT * FROM $conf[prefix_groups]groups WHERE id=$_GET[id]") or $db->err(__FILE__, __LINE__);

	//sprawdzanie czy grupa istnieje
	if($db->num_rows() == 0) {
		redirect('index.php?module=admin&action=groups');
		exit;
	}

	$d = $db->fetch_object();

	if( $d->type == -1 or get_gid_by_uid($_SESSION['id']) == $_GET['id'] ) {
		redirect('index.php?module=admin&action=groups');
		exit;
	}

	$yes='<input type="button" onClick="javascript:location.href=\'index.php?module=groups&function=group_delete&amp;group='.$d->id.'\'" value="'.$lang['yes'].'" class="button" />';
	$no='<input type="button" onClick="javascript:location.href=\'index.php?module=admin&amp;action=groups\'" value="'.$lang['no'].'" class="button" />';

	$nm = "name_$_SESSION[lang_short]";

	if($d->$nm == '') {
		$db2 = new dbquery;
		$db2->query("SELECT $conf[language] FROM $conf[prefix]lang WHERE name = 'lang'") or $db2->err(__FILE__, __LINE__);

		$lng = $db2->fetch_array();
		$lng = $lng[$conf['language']];

		$nm = "name_$lng";
	}

	$t = $d->$nm;

	$content = string_template($lang['admin_really_delete'], array('yes' => $yes, 'no' => $no, 'what' => read_text_rest($t)));
}
elseif($_GET['cmd'] == 'add') {
	if(!perms_check('users', 'gr_add')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$type = "	<option value=\"1\">$lang[groups_single_user]</option>
		<option value=\"2\">$lang[groups_redactors]</option>";

	$db2->query("SELECT * FROM $conf[prefix_ranks]ranks WHERE points= -1 ORDER BY name_$_SESSION[lang_short] ASC") or $db->err(__FILE__, __LINE__);

	$rank = "<option value=\"0\"$selected>$lang[ranks_std]</option>
		<option value=\"sep\">-------------</option>\n";

	while($rk = $db2->fetch_array()) {
		$nm = "name_$_SESSION[lang_short]";

		if($rk[$nm] == '') {
			$db2 = new dbquery;
			$db2->query("SELECT $conf[language] FROM $conf[prefix]lang WHERE name = 'lang'") or $db2->err(__FILE__, __LINE__);

			$lng = $db2->fetch_array();
			$lng = $lng[$conf['language']];

			$nm = "name_$lng";
		}

		$rk[$nm] = read_text_rest($rk[$nm]);

		$rank .= "<option value=\"$rk[id]\">$rk[$nm]</option>\n";
	}

	//add_moderator_options
	if(perms_check('forum', 'set_moderators'))
		$moderator_options=get_options_for_user_edit_moderators('', '', '&nbsp;&nbsp;', '->&nbsp;', '0', array());
	//


	$array = array('ed' => 1, 'rank' => $rank, 'function' => 'group_add', 'type' => $type, 'date' => date('Y-m-d H:i:s'), 'type' => $type, 'color' => '', 'submit' => $lang['admin_add'], 'moderator_options' => $moderator_options, 'prefix'=>'');

	$multi = NULL;

	$langs = get_lang_list();
	$languages = get_languages_list();

	$i = 0;

	foreach($langs as $key => $l) {
		$i++;
		if($l==$_SESSION['lang_short']) {
			$icon = 'collapse';
			$style = 'block';
		}
		else {
			$icon = 'expand';
			$style = 'none';
		}

		$multi .= string_template(read_file('admin/themes/groups_form_multi.php'), array('lang' => $l, 'language' => strtoupper($l), 'icon' => $GLOBALS['theme_path'].$GLOBALS['theme_img'][$icon], 'style' => $style, 'i' => $i));
		$array["name_$l"] = '';
		$array["desc_$l"] = '';
	}

	$content = string_template(read_file('admin/themes/groups_form.php'), array('multi' => $multi));
	$content = string_template($content, $array);
}
elseif($_GET['cmd'] == 'edit_perms') {

	if(!perms_check('users', 'gr_perms_edit')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}
	SQLvalidate($_GET['id']);
	$db->query("SELECT * FROM $conf[prefix_groups]groups WHERE id=$_GET[id]") or $db->err(__FILE__, __LINE__);

	//sprawdzanie czy grupa istnieje
	if($db->num_rows() == 0) {
		redirect('index.php?module=admin&action=groups');
		exit;
	}

	$group = $db->fetch_array();

	$content = string_template(read_file('admin/themes/groups_perms_header.php'), array('id' => $group['id']));

	$db2->query("SELECT * FROM $conf[prefix_groups]groups_pdefs ORDER BY name ASC") or $db->err(__FILE__, __LINE__);

	while($d = $db2->fetch_object()) {
		$type = explode(';', $d->types);
		$defs = explode(';', $d->defs);

		$nm = "names_$_SESSION[lang_short]";
		$ds = "desc_$_SESSION[lang_short]";

		if($d->$nm == '') {
			$nm = "names_".$conf['language'];
			$ds = "desc_".$conf['language'];
		}

		$names = explode(';', $d->$nm);
		$desc = explode(';', $d->$ds);

		$small_items = NULL;

		$values = explode(';', $group[$d->name]);

		for($i = 0; $i < count($defs); $i++) {
			switch($type[$i]) {
				case 'text':
					$field = '<input type="text" maxlength="150" name="'.$d->name.'__'.$defs[$i].'" value="'.$values[$i].'" class="button" size="30" />';
					break;
				case 'bool':
					$field = '<input type="checkbox" name="'.$d->name.'__'.$defs[$i].'" value="1" class="radio"';
					if($values[$i] == 1)
						$field .= ' checked';
					$field .= ' />';
					break;
				default:
					if(eregi("^select\{(.*)\}$", $type[$i], $opts)) {
						$field = "<select name=\"".$d->name."__".$defs[$i]."\" class=\"button\">\n";
						$options = explode(',', $opts[1]);
						for($k = 0; $k < count($options); $k++) {
							if(eregi("^\((.*)::(.*)\)$", $options[$k], $matches)) {
								if($values[$i] == $matches[1])
									$sel = ' selected';
								else
									$sel = NULL;
								$field .= "<option value=\"$matches[1]\"$sel>$matches[2]</option>\n";
							}
							else {
								if($values[$i] == $options[$k])
									$sel = ' selected';
								else
									$sel = NULL;
								$field .= "<option value=\"$options[$k]\"$sel>$options[$k]</option>\n";
							}
						}
						$field .= "</select>";
					}
					break;
			}
			$small_items .= string_template(read_file('admin/themes/groups_perms_small_item.php'), array('i' => (($i%2)+1), 'name' => $names[$i], 'desc' => $desc[$i], 'field' => $field, 'parent'=>$d->name, 'defs'=>$defs[$i]));
		}

		$content .= string_template(read_file('admin/themes/groups_perms_item.php'), array('name' => $d->name, 'small_items' => $small_items));
	}
	$content .= read_file('admin/themes/groups_perms_footer.php');
}

$content=string_template(read_file('middle.php'), array('theme_path' => $GLOBALS['theme_path'], 'content' => $content, 'description' => $lang['groups']));
//

?>
